package router

import (
	"apibe/grpc/rome/v1/router/handler"
	"apibe/grpc/rome/v1/service/impl"
	"apibe/grpc/rome/v1/share/crypt"
	"apibe/grpc/rome/v1/share/middler"
	"github.com/gin-gonic/gin"
)

func TaskUpsert(c *gin.Context) {
	var t impl.Task
	claims := c.MustGet(middler.CLAIMS).(*crypt.CustomClaims)
	err := c.ShouldBindJSON(&t)
	if err != nil {
		c.Set(middler.CODE, middler.ErrReq)
		c.Set(middler.MSG, err.Error())
		return
	}
	err = handler.TaskUpsertHandler(&t, claims)
	if err != nil {
		c.Set(middler.CODE, middler.ErrMgo)
		c.Set(middler.MSG, err.Error())
		return
	}
	c.Set(middler.RESULT, "SUCCESS")
}

func TaskFindMany(c *gin.Context) {
	var in struct {
		Filter   map[string]interface{} `json:"filter"`
		Page     int                    `json:"page"`
		PageSize int                    `json:"page_size"`
	}
	err := c.ShouldBindJSON(&in)
	if err != nil {
		c.Set(middler.CODE, middler.ErrReq)
		c.Set(middler.MSG, err.Error())
		return
	}
	taskList, count, err := handler.TaskFindManyHandler(in.Filter, in.Page, in.PageSize)
	if err != nil {
		c.Set(middler.CODE, middler.ErrMgo)
		c.Set(middler.MSG, err.Error())
		return
	}
	result := map[string]interface{}{
		"total": count,
		"data":  *taskList,
	}
	c.Set(middler.RESULT, result)
}
